<template>
  <div class="department-container">
    <FeatureHeader img="https://cdn-icons-png.flaticon.com/512/2991/2991148.png" title="部门管理" />
    <div class="toolbar">
      <el-button type="primary" @click="handleAdd">新增部门</el-button>
    </div>

    <el-table :data="departmentList" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="name" label="部门名称"></el-table-column>
      <el-table-column prop="manager" label="部门主管"></el-table-column>
      <el-table-column prop="employeeCount" label="员工数量"></el-table-column>
      <el-table-column prop="description" label="部门描述"></el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="部门主管" prop="manager">
          <el-select v-model="form.manager" placeholder="请选择部门主管">
            <el-option
              v-for="item in managers"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门描述" prop="description">
          <el-input type="textarea" v-model="form.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import FeatureHeader from '@/components/FeatureHeader.vue'
import BaseTable from '@/components/BaseTable.vue'
import BaseForm from '@/components/BaseForm.vue'
import { mapState, mapActions } from 'vuex'

export default {
  name: 'DepartmentList',
  components: { FeatureHeader, BaseTable, BaseForm },
  data() {
    return {
      dialogVisible: false,
      dialogTitle: '新增部门',
      form: {
        name: '',
        manager: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '请输入部门名称', trigger: 'blur' }],
        manager: [{ required: true, message: '请选择部门主管', trigger: 'change' }],
        description: [{ required: true, message: '请输入部门描述', trigger: 'blur' }]
      },
      managers: [
        { value: '张三', label: '张三' },
        { value: '李四', label: '李四' },
        { value: '王五', label: '王五' }
      ],
      loading: true,
      editMode: false
    }
  },
  computed: {
    ...mapState({
      allDepartments: state => state.department.list,
      employeeList: state => state.employee.list
    }),
    departmentList() {
      return this.allDepartments.map(department => {
        const employeeCount = this.employeeList.filter(employee => employee.department === department.name).length;
        return { ...department, employeeCount };
      });
    }
  },
  created() {
    this.fetchList()
    this.fetchEmployeeList()
    setTimeout(() => { this.loading = false }, 1000)
  },
  methods: {
    ...mapActions({
      fetchList: 'department/fetchList',
      fetchEmployeeList: 'employee/fetchList',
      addDepartment: 'department/addDepartment',
      editDepartment: 'department/editDepartment',
      deleteDepartment: 'department/deleteDepartment'
    }),
    handleAdd() {
      this.dialogTitle = '新增部门'
      this.editMode = false
      this.form = { name: '', manager: '', description: '' }
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.dialogTitle = '编辑部门'
      this.editMode = true
      this.form = { ...row }
      this.dialogVisible = true
    },
    handleDelete(row) {
      this.$confirm('确认删除该部门?', '提示', {
        type: 'warning'
      }).then(() => {
        this.deleteDepartment(row.id)
        this.$message.success('删除成功')
      })
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.editMode) {
            this.editDepartment({ ...this.form })
            this.$message.success('编辑成功')
          } else {
            this.addDepartment({ ...this.form })
            this.$message.success('新增成功')
          }
          this.dialogVisible = false
        }
      })
    }
  }
}
</script>

<style scoped>
.department-container {
  padding: 20px;
}

.toolbar {
  margin-bottom: 20px;
}
</style> 